<template>
	<view class="ercode-body">
		<image src="/static/img/ercode-bg.jpg" mode="" class="bg"></image>
		<image :src="shareCodeUrl" mode="" class="ercode"></image>
		<u-button type="success" class="save" @click="saveImg">保存图片</u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				mainid : "",
				shareCodeUrl : ""
			};
		},
		onLoad() {
			this.getOpenid();
		},
		methods:{
			getOpenid() {
				var that = this;
				uni.getStorage({
					key: 'user',
					success: function(res) {
						console.log("res",res);
						that.mainid = res.data.id;
						// that.mainid = "1519585814305316866";
						that.getShareCode();
					}
				});
			},
			getShareCode() {
				console.log("getMyInfo: " + this.mainid);
				this.$request.post('/agent/getTjCode/' + this.mainid ,{}).then((res) => {
					console.log('推荐码：', res)
					this.shareCodeUrl = res.data.data;
				})
			},
			saveImg(){
				uni.downloadFile({
					url: this.shareCodeUrl, //仅为示例，并非真实的资源
					success: (res) => {
						if (res.statusCode === 200) {
							console.log('下载成功',res);
							uni.saveImageToPhotosAlbum({
								filePath : res.tempFilePath,
								success: ((res) => {
									this.$u.toast("已保存到相册");
								})
							})
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.ercode-body{
		width: 100%;
		height: 100%;
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;
		.bg{
			position: absolute;
			top: 0;
			left: 0;
			height: 1200rpx;
			width: 100%;
		}
	}
	.ercode{
		width: 400rpx;
		height: 400rpx;
		margin: 200rpx auto;
		position: absolute;
		top: 145rpx;
		left: 185rpx;
		z-index: 9;
	}
	.save{
		width: 60%;
		position: fixed;
		bottom: 20rpx;
		left: 20%;
	}
</style>
